package net.esj.struts.dispatcher;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

import net.esj.struts.JarBasedActionConfigBuilder;

import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.opensymphony.xwork2.spring.SpringObjectFactory;

public class MelonDyncConfigurationFilter extends StrutsPrepareAndExecuteFilter implements ApplicationContextAware{
	
	public static Dispatcher DISPATCHER ;
	
	private ApplicationContext applicationContext;

    public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations();
        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig);
            init.initLogging(config);
            Dispatcher dispatcher = init.initDispatcher(config);
            init.initStaticContentLoader(config, dispatcher);

            prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
            DISPATCHER = dispatcher;
            SpringObjectFactory factory = new SpringObjectFactory();
            factory.setApplicationContext(applicationContext);
            JarBasedActionConfigBuilder acb = new JarBasedActionConfigBuilder(dispatcher.getConfigurationManager().getConfiguration(),dispatcher.getContainer(), factory);
            
        } finally {
            init.cleanup();
        }

    }

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}
